package com.zyc.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyc.commonUtils.JwtUtils;
import com.zyc.commonUtils.R;
import com.zyc.eduservice.client.OrderClient;
import com.zyc.eduservice.entity.EduCourse;
import com.zyc.eduservice.entity.vo.ChapterVo;
import com.zyc.eduservice.entity.vo.CourseInfoVo;
import com.zyc.eduservice.entity.vo.CoursePublishVo;
import com.zyc.eduservice.entity.vo.EduCourseQuery;
import com.zyc.eduservice.service.EduChapterService;
import com.zyc.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin //解决跨域
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;



    @ApiOperation(value = "添加课程信息")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoVo courseInfo) {
        String courseId = eduCourseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId",courseId);
    }

    @ApiOperation(value = "获取指定id课程信息")
    @GetMapping("{id}")
    public R getCourseInfoById(@ApiParam(name = "id", value = "课程ID", required = true) @PathVariable String id) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfoById(id);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PutMapping("update/{id}")
    public R update(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody CourseInfoVo courseInfo){

        eduCourseService.updateById(id,courseInfo);
        return R.ok().data("courseId",id);
    }

    @ApiOperation(value = "获取指定id发布信息")
    @GetMapping("getPublishInfo/{id}")
    public R getPublishInfo(@ApiParam(name = "id", value = "课程ID", required = true) @PathVariable String id) {
        CoursePublishVo publishInfo = eduCourseService.getPublishInfo(id);
        return R.ok().data("publishInfo",publishInfo);
    }

    @ApiOperation(value = "最终发布")
    @GetMapping("publish/{id}")
    public R publish(@ApiParam(name = "id", value = "课程ID", required = true) @PathVariable String id) {
        eduCourseService.publish(id);
        return R.ok();
    }

    @ApiOperation(value = "分页讲师列表")
    @PostMapping("page/{page}/{limit}")
    public R courseList(
            @ApiParam(name = "page", value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数",required = true) @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象",required = false) @RequestBody EduCourseQuery courseQuery) {

        Page<EduCourse> coursePage = new Page<>(page,limit);

        eduCourseService.pageQuery(coursePage,courseQuery);
        List<EduCourse> records = coursePage.getRecords();
        long total = coursePage.getTotal();

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("根据id删除课程所有信息")
    @DeleteMapping("removeCourse/{id}")
    public R removeCourse(@ApiParam(name = "id", value = "课程ID", required = true) @PathVariable String id) {
        boolean flag = eduCourseService.removeCourse(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }


}

